package com.bootdo.order.controller;

import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.common.annotation.IpFilter;
import com.bootdo.common.annotation.Log;
import com.bootdo.common.redis.lock.CommonRedisLock;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;
import com.bootdo.order.domain.OrderPayDO;
import com.bootdo.order.domain.RecoverDO;
import com.bootdo.order.service.OrderPayeeService;
import com.bootdo.payee.domain.PayeeAgentDO;
import com.bootdo.payee.domain.PayeeDO;

/**
 * @Desc 收款户相关订单
 * @date 2019/12/10
 *
 */
@Controller
@RequestMapping("/orders/payee")
public class OrderPayeeController {

    private final String PREFIX = "orders/payee";
    @Autowired
    private OrderPayeeService orderPayeeService;

    @Autowired
    protected StringRedisTemplate stringRedisTemplate;

    @GetMapping()
    @RequiresPermissions("orders:payee:order")
    String order(Model model) {
        return PREFIX + "/list";
    }

    @ResponseBody
    @PostMapping("/findAgent")
    @RequiresPermissions("orders:payee:order")
    public R findAgent(){
        List<PayeeAgentDO> list = orderPayeeService.findAgent();
        return R.ok(list);
    }

    @ResponseBody
    @PostMapping("/findPayee")
    @RequiresPermissions("orders:payee:order")
    public R findPayee(Long userId){
        List<PayeeDO> list = orderPayeeService.findPayee(userId);
        return R.ok(list);
    }

    @ResponseBody
    @GetMapping("/list")
    @RequiresPermissions("orders:payee:order")
    public PageUtils list(@RequestParam Map<String, Object> params){
        //查询列表数据
        Query query = new Query(params);
        List<OrderPayDO> userList = orderPayeeService.list(query);
        int total = orderPayeeService.count(query);
        PageUtils pageUtils = new PageUtils(userList, total);
        return pageUtils;
    }


    @Log("追单")
    @GetMapping("/editTag/{id}")
    @RequiresPermissions("orders:payee:edit")
    String editTag(Model model, @PathVariable("id") Long id) {
        model.addAttribute("id", id);
        return PREFIX + "/editTag";
    }

    @IpFilter
    @ResponseBody
    @Log("补单")
    @PostMapping("/edit")
    @RequiresPermissions("orders:payee:edit")
    public R edit(OrderPayDO orderPay){
        CommonRedisLock commonLock = new CommonRedisLock(stringRedisTemplate, "ORDERPAYEEEDIT" + orderPay.getId());
        try {
            if (commonLock.tryLock(3)) {
                int count = orderPayeeService.edit(orderPay);
                if (count > 0) {
                    commonLock.unlock();
                    return R.ok();
                } else if (count == -2){
                    commonLock.unlock();
                    return R.error("订单已付款，请勿补单");
                } else {
                    commonLock.unlock();
                    return R.error();
                }
            }
        } catch (Exception e) {
            commonLock.unlock();
            return R.error();
        }
        return R.error();
    }

    @Log("追单")
    @GetMapping("/recover/{id}")
    @RequiresPermissions("orders:payee:recover")
    String recover(Model model, @PathVariable("id") Long id) {
        model.addAttribute("id", id);
        return PREFIX + "/recover";
    }

    @IpFilter
    @ResponseBody
    @Log("追单申请")
    @PostMapping("/recover/save")
    @RequiresPermissions("orders:payee:recover")
    public R recoverSave(RecoverDO recover){
        CommonRedisLock commonLock = new CommonRedisLock(stringRedisTemplate, "ORDERRECOVER" + recover.getOrderId());
        try {
            if (commonLock.tryLock(3)) {
                if (orderPayeeService.recoverSave(recover) > 0) {
                    return R.ok();
                }
            }
            return R.error();
        } finally {
            commonLock.unlock();
        }
    }


    @Log("查看二维码图片")
    @GetMapping("/showImg/{id}")
    @RequiresPermissions("orders:payee:order")
    String showImg(Model model, @PathVariable("id") Long id) {
        OrderPayDO orderPayDO = orderPayeeService.get(id);
        model.addAttribute("id", id);
        model.addAttribute("orderPayDO", orderPayDO);
        return PREFIX + "/showImg";
    }

}
